pacman::p_load(tidyverse, plotly, gganimate, gifski, gapminder, readxl)Programming Animated Statistical Graphics with R
1. Learning Outcome
In this hands-on exercise, we will learn to
create animated data visualization by using gganimate and plotly R packages
reshape data using tidyr package
process, wrangle and transform data using dplyr package
2. Getting Started
2.1 Installing and loading the required libraries
Firstly, let’s install and load the required packages:
tidyverse: an opinionated collection of R packages designed for data import, data wrangling and data exploration
plotly: to plot interative statistical graphs
gganimate: to plot animated visualization
gifski: to convert images to GIF animations
gapminder: An excerpt of the data available at Gapminder.org. We’ll use its country_colors scheme.
readxl: to read data from excel files
2.2 Importing the data
In this hands-on exercise, we’ll use the Data worksheet from GlobalPopulation Excel workbook
Let’s start by importing the data.
col <- c("Country", "Continent")
globalPop <- read_xls("../../Data/GlobalPopulation.xls",
sheet = "Data") %>%
mutate_each_(funs(factor(.)), col) %>% # to convert all character data into factor
mutate(Year = as.integer(Year)) # to convert Year column into integerNote that mutate_each_() was deprecated in dplyr 0.7.0. and funs() was deprecated in dplyr 0.8.0. Therefore, we will replace the function with mutate_at() function. Alternatively, across() function can also be used to achieve the same output.
Example code using mutate_at() function.
col <- c("Country", "Continent")
globalPop <- read_xls("../../Data/GlobalPopulation.xls",
sheet = "Data") %>%
mutate_at(col, as.factor) %>%
mutate(Year = as.integer(Year))Example code using across() function.
col <- c("Country", "Continent")
globalPop <- read_xls("../../Data/GlobalPopulation.xls",
sheet = "Data") %>%
mutate(across(col, as.factor)) %>%
mutate(Year = as.integer(Year))3. Animated Data Visualisation: gganimate methods
Now, let’s use gganimate methods to create some animated visualizations. The visualizations are generated using the following functions:
transition_(): defines how the data should be spread out and how it relates to itself across time.
view_(): defines how the positional scales should change along the animation.
shadow_(): defines how data from other points in time should be presented in the given point in time.
enter_()/exit_(): defines how new data should appear and how old data should disappear during the course of the animation.
ease_aes(): defines how different aesthetics should be eased during transitions.
3.1 Building a static population bubble plot
Let’s first create a static bubble plot using ggplot2 method.
ggplot(globalPop, aes(x = Old,
y = Young,
size = Population,
colour = Country)) +
geom_point(alpha = 0.7,
show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}',
x = '% Aged',
y = '% Young') 
3.2 Building the animated bubble plot
Next, let’s make the bubbles move by using the two functions below:
transition_time(): to create transition through distinct states in time (i.e. Year).
ease_aes(): to control easing of aesthetics. The default is linear. Other methods are: quadratic, cubic, quartic, quintic, sine, circular, exponential, elastic, back, and bounce.
ggplot(globalPop, aes(x = Old,
y = Young,
size = Population,
colour = Country)) +
geom_point(alpha = 0.7,
show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}',
x = '% Aged',
y = '% Young') +
transition_time(Year) +
ease_aes('linear') 
4. Animated Data Visualisation: plotly
Now, let’s make the animated visualization interactive by using ggplotly() method.
4.1 Building an animated bubble plot: ggplotly() method
gg <- ggplot(globalPop,
aes(x = Old,
y = Young,
size = Population,
colour = Country)) +
geom_point(aes(size = Population,
frame = Year),
alpha = 0.7,
show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(x = '% Aged',
y = '% Young')
ggplotly(gg)Although we have turned off legend using show.legend = FALSE argument, it stills shows in the plot. To overcome this problem, theme(legend.position=‘none’) should be used as shown in the plot and code chunk below.
gg <- ggplot(globalPop,
aes(x = Old,
y = Young,
size = Population,
colour = Country)) +
geom_point(aes(size = Population,
frame = Year),
alpha = 0.7) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(x = '% Aged',
y = '% Young') +
theme(legend.position = 'none')
ggplotly(gg)4.2 Building an animated bubble plot: plot_ly() method
Another way to make animated bubble plot is to use plot_ly() function.
bp <- globalPop %>%
plot_ly(x = ~Old,
y = ~Young,
size = ~Population,
color = ~Continent,
sizes = c(2, 100),
frame = ~Year,
text = ~Country,
hoverinfo = "text",
type = 'scatter',
mode = 'markers'
) %>%
layout(showlegend = FALSE)
bpThis comes to the end of this hands-on exercise. I have learned many different methods to create animated visualizations in R. Hope you enjoyed it, too!
See you in the next hands-on exercise 🥰